package com.yunhe.commens.filterservlet;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 字符集编码过滤器
 */
@WebFilter(filterName = "charsetfilter" ,urlPatterns = "/*") //*表示全限定
public class Charsetservlet implements Filter {
    String charset;//项目字符集编码
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding ( charset );
        filterChain.doFilter ( servletRequest,servletResponse );//继续往下执行
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
         charset = filterConfig.getServletContext ().getInitParameter ( "charset" );//获取参数设置中的字符集编码
         charset = null==charset ? "utf-8":charset;//判断是否为空
    }

    @Override
    public void destroy() {}
}
